package com.ruoyi.web.controller.system;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayOpenAppQrcodeCreateModel;
import com.alipay.api.domain.AlipayOpenMiniQrcodeBindModel;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.request.AlipayOpenMiniQrcodeBindRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.alipay.api.response.AlipayOpenMiniQrcodeBindResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.ruoyi.common.config.AliConfig;
import com.ruoyi.common.config.AlipayToken;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ParkPrice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.ruoyi.common.config.AliConfig.alipayClient;

@RestController
@RequestMapping("/aLiJsApiPay")
public class AliJsApiPayController {

    @GetMapping("/getQrcodeBind")
    public AjaxResult getQrcodeBind() throws AlipayApiException {
        AlipayOpenMiniQrcodeBindRequest request = new AlipayOpenMiniQrcodeBindRequest();
        AlipayOpenMiniQrcodeBindModel model = new AlipayOpenMiniQrcodeBindModel();
        model.setRouteUrl("https://www.yoursite.com/");
        model.setPageRedirection("pages/index/index");
        model.setMode("FUZZY");
        model.setRoutePattern("http://yoursite.com/merchant/{0}/shop/{1}");
        request.setBizModel(model);
        AlipayOpenMiniQrcodeBindResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return AjaxResult.success();
    }

    @GetMapping("/getQrcodeCreate")
    public AjaxResult getQrcodeCreate(){

        // 构造请求参数以调用接口
        AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
        AlipayOpenAppQrcodeCreateModel model = new AlipayOpenAppQrcodeCreateModel();

        // 设置跳转小程序的页面路径
        model.setUrlParam("page/login");

        // 设置小程序的启动参数
        model.setQueryParam("x=1");

        // 设置码描述
        model.setDescribe("二维码描述");

        request.setBizModel(model);
        try {
            AlipayOpenAppQrcodeCreateResponse response = alipayClient.execute(request);
            return AjaxResult.success("访问成功",response.getQrCodeUrl());
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/create")
    public AjaxResult jsApiCreate(@RequestBody ParkPrice parkPrice){

        // 构造请求参数以调用接口
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        AlipayTradeCreateModel model = new AlipayTradeCreateModel();

        // 设置商户订单号
        model.setOutTradeNo(AliConfig.generateOrderNumber("ORD"));

        // 设置产品码
        model.setProductCode("JSAPI_PAY");

        // 设置小程序支付中
        model.setOpAppId(AliConfig.appId);

        // 设置订单总金额
        model.setTotalAmount(parkPrice.getPrice());

        // 设置可打折金额
//        model.setDiscountableAmount("80.00");

        // 设置订单标题
        model.setSubject("中央缴费");

        // 设置订单附加信息
//        model.setBody("Iphone6 16G");

        // 设置买家支付宝用户ID
        model.setBuyerId(AlipayToken.userId);

        // 设置商户门店编号
//        model.setStoreId("NJ_001");

        request.setBizModel(model);
        AlipayTradeCreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("out_trade_no",response.getOutTradeNo());
            ajaxResult.put("trade_no",response.getTradeNo());
            return ajaxResult;
        } catch (AlipayApiException e) {
            return AjaxResult.error("error",e);
        }
    }
    @GetMapping("/userid")
    public ResponseEntity<String> getUserId() {
        if (AlipayToken.userId.length()>0){
            return ResponseEntity.ok(AlipayToken.userId);
        }
        return ResponseEntity.ok("查询失败");
    }
}
